import java.util.List;
import java.util.Scanner;

public class Demo {
    private static Scanner scanner = new Scanner(System.in);
    private static UserDAO userDAO = new UserDAO();
    private static StudentDAO studentDAO = new StudentDAO();

    public static void main(String[] args) {
        boolean running = true;
        while (running) {
            showLoginMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    if (login()) {
                        showStudentManagementMenu();
                    } else {
                        System.out.println("登录失败，用户名或密码错误！");
                    }
                    break;
                case 2:
                    register();
                    break;
                case 3:
                    running = false;
                    System.out.println("感谢使用，再见！");
                    break;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
        scanner.close();
    }

    private static void showLoginMenu() {
        System.out.println("\n=== 学生管理系统 ===");
        System.out.println("1. 登录");
        System.out.println("2. 注册");
        System.out.println("3. 退出");
        System.out.print("请选择操作: ");
    }

    private static boolean login() {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();
        return userDAO.validateUser(username, password);
    }

    private static void register() {
        while (true) {
            System.out.print("请输入用户名: ");
            String username = scanner.nextLine();

            if (userDAO.checkUsernameExists(username)) {
                System.out.println("用户名已存在，请重新输入！");
                continue;
            }

            String password1, password2;
            while (true) {
                System.out.print("请输入密码: ");
                password1 = scanner.nextLine();
                System.out.print("请再次输入密码: ");
                password2 = scanner.nextLine();

                if (!password1.equals(password2)) {
                    System.out.println("两次输入的密码不一致，请重新输入！");
                } else {
                    break;
                }
            }

            User user = new User(username, password1);
            if (userDAO.registerUser(user)) {
                System.out.println("注册成功！请登录。");
                break;
            } else {
                System.out.println("注册失败，请重试！");
            }
        }
    }

    private static void showStudentManagementMenu() {
        boolean running = true;
        while (running) {
            System.out.println("\n=== 学生管理系统 ===");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生信息");
            System.out.println("4. 查看所有学生");
            System.out.println("5. 按ID查找学生");
            System.out.println("6. 返回登录菜单");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    viewAllStudents();
                    break;
                case 5:
                    viewStudentById();
                    break;
                case 6:
                    running = false;
                    System.out.println("已返回登录菜单！");
                    break;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void addStudent() {
        System.out.println("\n=== 添加学生 ===");
        System.out.print("姓名: ");
        String name = scanner.nextLine();
        System.out.print("年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        System.out.print("性别: ");
        String gender = scanner.nextLine();
        System.out.print("年级: ");
        String grade = scanner.nextLine();

        Student student = new Student(name, age, gender, grade);
        if (studentDAO.addStudent(student)) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    private static void deleteStudent() {
        System.out.println("\n=== 删除学生 ===");
        System.out.print("请输入要删除的学生ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        if (studentDAO.deleteStudent(id)) {
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败，未找到该学生！");
        }
    }

    private static void updateStudent() {
        System.out.println("\n=== 修改学生信息 ===");
        System.out.print("请输入要修改的学生ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        Student student = studentDAO.getStudentById(id);
        if (student == null) {
            System.out.println("未找到该学生！");
            return;
        }

        System.out.println("当前学生信息: " + student);

        System.out.print("新姓名 (留空不修改): ");
        String name = scanner.nextLine();
        if (!name.isEmpty()) {
            student.setName(name);
        }

        System.out.print("新年龄 (留空不修改): ");
        String ageInput = scanner.nextLine();
        if (!ageInput.isEmpty()) {
            student.setAge(Integer.parseInt(ageInput));
        }

        System.out.print("新性别 (留空不修改): ");
        String gender = scanner.nextLine();
        if (!gender.isEmpty()) {
            student.setGender(gender);
        }

        System.out.print("新年级 (留空不修改): ");
        String grade = scanner.nextLine();
        if (!grade.isEmpty()) {
            student.setGrade(grade);
        }

        if (studentDAO.updateStudent(student)) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
    }

    private static void viewAllStudents() {
        System.out.println("\n=== 所有学生信息 ===");
        List<Student> students = studentDAO.getAllStudents();
        if (students.isEmpty()) {
            System.out.println("暂无学生信息！");
        } else {
            for (Student student : students) {
                System.out.println(student);
            }
        }
    }

    private static void viewStudentById() {
        System.out.println("\n=== 按ID查找学生 ===");
        System.out.print("请输入学生ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        Student student = studentDAO.getStudentById(id);
        if (student == null) {
            System.out.println("未找到该学生！");
        } else {
            System.out.println(student);
        }
    }
}